// 所有couter组件的的业务逻辑都在这里进行管理
//逻辑更改数据
import {
  compute
} from '../libs/utils';

function calculatorReducer(data) {
  function setNumber(field, value) {
    if (typeof (value) !== 'number') {
      value = Number(value);
    }

    //data.firstName / data.secondName
    data[field] = value;

    return compute(
      data.curMethod,
      data.firstNumber,
      data.secondNumber
    );
  }

  function changeMethod(method) {
    data.curMethod = method;
    return compute(
      data.curMethod,
      data.firstNumber,
      data.secondNumber
    );
  }

  return {
    setNumber,
    changeMethod
  }
}

export default calculatorReducer;